<?php

namespace App\Http\Common;


use App\Http\Models\Admin\CMS\Category;
use App\Http\Models\Admin\File\FileContent;
use App\Http\Models\Admin\Sys\SysParam;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class Constant {

      public  static  function  initCategory(){

            $list = FileContent::select('id','title','name','parent_id')->whereNull('parent_id')->orWhere('parent_id',0)->where('is_show',1)->orderBy('show_index','asc')->get();
            foreach($list as $item){
                Constant::initCategoryData($item);
            }
            return $list;
        }

        public static function initCategoryData($data)
        {
            //
            $data->children =  FileContent::select('id','title','name','parent_id')->where('parent_id',$data->id)->where('is_show',1)->orderBy('show_index','asc')->get();

            if($data->children == null){
                return;
            }
            foreach($data->children as $child){
                Constant::initCategoryData($child);
            }

        }


    /**
     * 裁剪字符串,指定宽带.>宽带字段补全
     * @param $str
     * @param int $length
     * @param string $append
     * @return string
     */
    public static function subStr($str,$length=200,$append="...")
    {
        $str = trim(strip_tags($str));
        if(mb_strlen($str) > $length){
            $p_content = mb_substr($str,0,$length,'utf-8');
            $str = $p_content.$append;
        }
        return $str;
    }

    /** 日期格式化
     * @param $datestr
     * @param string $format
     * @return string
     */
    public static function dateFormat($date)
    {
        return array(
            'year'=>date('Y',strtotime($date)),
            'month'=>date('m',strtotime($date)),
            'day'=>date('d',strtotime($date)),
            'hour'=>date('H',strtotime($date)),
            'minute'=>date('i',strtotime($date)),
        );
    }


    public static function needUrl($str){
        return starts_with($str,'/uploads') ;
    }


    /**
     * 随机字符串
     * @param $length
     * @return null|string
     */
    public static function getRandChar($length){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol)-1;

        for($i=0;$i<$length;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $str;
    }



    public static function listFolderFiles($directory,&$result)
    {
        $mydir = dir($directory);
        while($file = $mydir->read())
        {
            if(is_dir("$directory/$file")&&$file!="."&&$file!="..")
            {
                Constant::listFolderFiles("$directory/$file",$result);
            }
            else{
                if($file!="." && $file!=".."&& $file!=""){
                    $result[] = array(
                        'filename'=>$file,
                        'fullname'=>str_replace(base_path(),'',$directory."/".$file),
                        'folder'=>$directory
                    );
                }
            }

        }
        $mydir->close();
    }
}
?> 
